import { _decorator, Button, Label, Node, v3 } from 'cc';
import { BasePopUp } from '../../../A-FRAME/component/ui.pop-up';
import { UserData } from '../../../Data/UserData';
import { AudioMgr } from '../../../Mgr/AudioMgr';
import { GameMgr } from '../../../Mgr/GameMgr';
import { UIMgr } from '../../../Mgr/UIMgr';
import LocalizedLabel from '../../../Tools/i18n/LocalizedLabel';
import { Url } from '../../../Tools/Url';
import { Popup } from '../page/popup';
const { ccclass, property } = _decorator;

@ccclass('GetlottwardPage')
export class GetlottwardPage extends BasePopUp<typeof Popup, { type: string, value: number }> {
    protected default_return: { type: string; value: number; };

    protected pageConfig: { awardType: number, needPassLevel: number, awardAmount: number, awardNum: number, cashTotal: number } = null;
    @property(Node)
    ward: Node = null;
    @property(Label)
    wardLabel: Label = null;
    @property(Label)
    dowenLabel: Label = null;


    public onShow(config: typeof this.pageConfig): void {
        AudioMgr.ins.playOneShot(Url.AUDIO.SFX11, 1);
        this.pageConfig = config;
        BasePopUp.jelly_enter(this.node);
        this.renderLocalUI()
    }

    protected _click_event_: { [name: string]: (this: GetlottwardPage, button: Button) => void; } = {
        getBtn(btn) {
            AudioMgr.ins.playOneShot(Url.AUDIO.SFX2, 1);
            btn.interactable = false;
            BasePopUp.jelly_leave(this.node).then(() => {//TODO  加钱逻辑
                if (this.pageConfig.awardType == 1) {
                    UIMgr.ins.updateCashLabel(this.pageConfig.cashTotal);
                    UIMgr.ins.shwoGetCashEffect(v3(0, 0, 0));
                } else {
                    UserData.data.propNum++;
                    UIMgr.ins.freshLightState();
                }
                GameMgr.ins.startTimer();
                this.close(null);
                btn.interactable = true;
            });
        },
        closeBtn(btn) {
            AudioMgr.ins.playOneShot(Url.AUDIO.SFX2, 1);
            btn.interactable = false;
            BasePopUp.jelly_leave(this.node).then(() => {//TODO  加钱逻辑
                if (this.pageConfig.awardType == 1) {
                    UIMgr.ins.updateCashLabel(this.pageConfig.cashTotal);
                    UIMgr.ins.shwoGetCashEffect(v3(0, 0, 0));
                } else {
                    UserData.data.propNum++;
                    UIMgr.ins.freshLightState();
                }
                GameMgr.ins.startTimer();
                this.close(null);
                btn.interactable = true;
            });
        }
    }


    renderLocalUI() {
        if (this.pageConfig.awardType == 1) {
            this.ward.children[0].active = false;
            this.ward.children[1].active = true;
            this.wardLabel.string = '+' + UIMgr.ins.formatCashValue(this.pageConfig.awardAmount);
        } else {
            this.ward.children[0].active = true;
            this.ward.children[1].active = false;
            this.wardLabel.string = '+' + this.pageConfig.awardNum;
        }

        let localizedLabel = this.dowenLabel.node.getComponent(LocalizedLabel);
        localizedLabel.setTextKeyAndOption("pop_lotterypage_cond_text", this.pageConfig.needPassLevel);
    }
}

